/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aiaudio.processing.reduce.klsh.kmeaning.base;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author nastya
 */
public abstract class AbstractKMeaningAlgorithm <ADGT extends AlgorithmDataGrid>{   
    private int l;
    private int k;

    public int getK() {
        return k;
    }

    public int getL() {
        return l;
    }

    public AbstractKMeaningAlgorithm(int l, int k) {
        this.l = l;
        this.k = k;
    }
    
    private ADGT grid;
    
    public void initialize(List<Data> trainingSet){
        grid = createAlgorithmDataGrid();
        for (int i = 0; i < l; i++) {
            List<Data> randomValues = collectRandomValues(trainingSet, k);
            grid.addCodepage(randomValues);
        }
    }

    private List<Data> collectRandomValues(List<Data> trainingSet, int k) {
        int i = 0;
        List<Data> result = new Vector<Data>();
        Random r = new Random();
        
        while(i < k){
            int idx = r.nextInt(k);
            Data d = result.get(idx);
            if(!result.contains(d)){
                result.add(d);
                i++;
            }
        }
        
        return result;
    }
    
    public HashMap<Long, Data> testVector(Data vector){
        try {
            return grid.testVector(vector);
        } catch (IOException ex) {
            Logger.getLogger(AbstractKMeaningAlgorithm.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    protected abstract ADGT createAlgorithmDataGrid();
    
}
